package com.zuimei.zuimei.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Display;

import com.zuimei.zuimei.R;

/**
 * Created by Jogger on 2017/1/23.
 */

public class URLDrawable extends BitmapDrawable {
    protected Drawable drawable;
    private Context mContext;

    @SuppressWarnings("deprecation")
    public URLDrawable(Context context) {
        mContext = context;
        this.setBounds(getDefaultImageBounds(context));
        drawable = context.getResources().getDrawable(R.mipmap.ic_launcher);
        drawable.setBounds(getDefaultImageBounds(context));
    }

    @Override
    public void draw(Canvas canvas) {
        //设置图片宽高
        int width = ScreenUtil.getSrceenWidth(mContext) - 60;
        int height = (int) (width / ((float) drawable.getIntrinsicWidth() / drawable
                .getIntrinsicHeight()));
        drawable.setBounds(30, 0, width, height);
        drawable.draw(canvas);
    }

    @SuppressWarnings("deprecation")
    public Rect getDefaultImageBounds(Context context) {
        Display display = ((Activity) context).getWindowManager()
                .getDefaultDisplay();
        int width = display.getWidth();
        int height = (int) (width * 1.6f);
        return new Rect(0, 0, width, height);
    }
}
